package cn.com.pajx.pajx_visitor_v3.ui.activity;

import android.annotation.SuppressLint;
import android.text.TextUtils;
import android.view.View;
import android.widget.LinearLayout;

import androidx.annotation.NonNull;
import androidx.recyclerview.widget.RecyclerView;

import com.google.gson.Gson;
import com.rcw.swiperefreshrecyclerview.BaseAdapter;

import org.greenrobot.eventbus.EventBus;
import org.greenrobot.eventbus.Subscribe;
import org.greenrobot.eventbus.ThreadMode;

import java.util.ArrayList;
import java.util.LinkedHashMap;
import java.util.List;

import butterknife.BindView;
import butterknife.OnClick;
import cn.com.pajx.pajx_visitor_v3.R;
import cn.com.pajx.pajx_visitor_v3.adapter.VisiteAdapter;
import cn.com.pajx.pajx_visitor_v3.api.Api;
import cn.com.pajx.pajx_visitor_v3.base.BaseRecyclerViewActivity;
import cn.com.pajx.pajx_visitor_v3.bean.EventMessage;
import cn.com.pajx.pajx_visitor_v3.bean.VisiteBean;
import cn.com.pajx.pajx_visitor_v3.bean.VisitePageBean;
import cn.com.pajx.pajx_visitor_v3.ui.fragment.StartVisiteFragment;
import cn.com.pajx.pajx_visitor_v3.ui.view.dialog.CommonDialog;
import cn.com.pajx.pajx_visitor_v3.utils.AppConstant;

/**
 * 预约查询页面
 */
@SuppressLint("NonConstantResourceId")
public class OrderActivity extends BaseRecyclerViewActivity<VisiteBean> {

    @BindView(R.id.view_empty)
    LinearLayout viewEmpty;

    private final List<VisiteBean> visitorBeans = new ArrayList<>();
    private VisiteAdapter mAdapter;
    private String phone, visite_id;
    private int totalPage;

    @Override
    protected int getLayoutId() {
        return R.layout.activity_order;
    }

    @Override
    protected void beforeSetContentView() {
        super.beforeSetContentView();
        phone = getIntent().getStringExtra(AppConstant.VISITE_PHONE);
    }

    @Override
    protected String loadTag() {
        return "ORDER_RECORD";
    }

    @Override
    protected String getApiUrl(boolean isRefresh) {
        if (isRefresh) visitorBeans.clear();
        paramsMap.put("vis_phone", phone);
        return Api.ORDER_RECORD;
    }

    @Override
    protected void initView() {
        super.initView();
        setHeadTitle("今日预约");
        EventBus.getDefault().register(this);
        initDownTimer();
        mAdapter.setOnFunctionListener(bean -> {
            visite_id = bean.getVis_id();
            cancelDownTimer();
            if (TextUtils.equals("1", bean.getVis_status())) {
                StartVisiteFragment startVisiteFragment = StartVisiteFragment.newInstance(bean);
                startVisiteFragment.show(getSupportFragmentManager(), "startVisiteFragment");
            } else {
                showEndVisiteView();
            }

        });
        xRecyclerView.setOnScrollListener(new RecyclerView.OnScrollListener() {
            @Override
            public void onScrollStateChanged(@NonNull RecyclerView recyclerView, int newState) {
                super.onScrollStateChanged(recyclerView, newState);
                resetDownTimer();
            }
        });
    }

    @Override
    protected BaseAdapter<VisiteBean> getAdapter() {
        return mAdapter = new VisiteAdapter(mContext, R.layout.visite_item, visitorBeans);
    }

    @Override
    protected void showData(String data, String tag) {
        if (TextUtils.equals(tag, "ORDER_RECORD")) {
            VisitePageBean visitePageBean = new Gson().fromJson(data, VisitePageBean.class);
            if (visitePageBean != null) {
                totalPage = visitePageBean.getPages();
                visitorBeans.addAll(visitePageBean.getRecords() == null ? new ArrayList<>() : visitePageBean.getRecords());
                showViewData();
            }
        } else {
            skip2Success("2");
            finish();
        }
    }

    private void showEndVisiteView() {
        new CommonDialog(mContext)
                .setTitle("结束拜访")
                .setMessage("确定要结束拜访吗？结束后请尽快离校！")
                .setChildClickListener(commonDialog -> {
                    finishVisite();
                    commonDialog.dismiss();
                })
                .show();
    }

    /**
     * 结束拜访
     */
    private void finishVisite() {
        LinkedHashMap<String, String> paramsMap = new LinkedHashMap<>();
        paramsMap.put("vis_id", visite_id);
        mPresenter.loadData(Api.VISITE_FINISH, paramsMap, "VISITE_FINISH", "");
    }

    @SuppressLint("NotifyDataSetChanged")
    public void showViewData() {
        if (visitorBeans.isEmpty()) {
            viewEmpty.setVisibility(View.VISIBLE);
        } else {
            mAdapter.notifyDataSetChanged();
            viewEmpty.setVisibility(View.GONE);
        }
    }

    @Override
    protected int getTotalPage() {
        return totalPage;
    }

    @Override
    protected boolean getLoadMoreEnable() {
        return true;
    }

    @Override
    protected void loadLocalData() {

    }

    @OnClick(R.id.tv_back)
    public void onViewClicked() {
        finish();
    }

    @Subscribe(threadMode = ThreadMode.MAIN)
    public void onEventBusMessage(EventMessage bean) {
        if (TextUtils.equals(AppConstant.FRAGMENTDIALOG_DISMISS, bean.getMessage())) {
            resetDownTimer();
        } else if (TextUtils.equals(AppConstant.VISITE_START, bean.getMessage())) {
            finish();
        }
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        EventBus.getDefault().unregister(this);
    }

}